package com.dimanche.module_course.ui.main;

import android.os.Bundle;
import android.text.Html;
import android.view.View;

import androidx.core.text.HtmlCompat;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelProviders;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dimanche.library_base.constant.RouterPath;
import com.dimanche.library_base.utils.AppUtils;
import com.dimanche.library_base.utils.SystemTTS;
import com.dimanche.library_db.entity.WordEntity;
import com.dimanche.module_course.BR;
import com.dimanche.module_course.R;
import com.dimanche.module_course.app.AppViewModelFactory;
import com.dimanche.module_course.databinding.CourseActivityMainBinding;

import java.util.List;

import me.goldze.mvvmhabit.base.BaseActivity;

/**
 * author : Dimanche
 * date   : 2021/3/26 16:23
 * desc   :https://blog.csdn.net/qq_39085422/article/details/78612132
 * version: 1.0
 */
@Route(path = RouterPath.Course.AC_MAIN)
public class MainActivity extends BaseActivity<CourseActivityMainBinding, MainViewModel> {
    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.course_activity_main;
    }

    @Override
    public int initVariableId() {
        return BR.model;
    }

    @Override
    public MainViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(AppUtils.getApplication());
        return ViewModelProviders.of(this, factory).get(MainViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.getFirstWord();
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.datas.observe(this, listLiveData -> {
            listLiveData.observe(this, wordEntities -> {
                if (wordEntities.size() > 0) {
                    WordEntity wordEntity = wordEntities.get(0);
                    binding.word.setText(wordEntity.word);
                    binding.pinyin.setText(wordEntity.pinyin);
                    binding.strokes.setText(wordEntity.strokes + "");
                    binding.radicals.setText(wordEntity.radicals);
                    binding.explanation.setText(Html.fromHtml(wordEntity.explanation));
                }
            });
        });

    }
}
